/**
 */
package com.dingpeikeji.quickdeer.core.modules.orders.service;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.finance.entity.TFinance;
import com.dingpeikeji.quickdeer.core.modules.orders.dao.TOrdersDao;
import com.dingpeikeji.quickdeer.core.modules.orders.entity.TOrders;
import com.dingpeikeji.quickdeer.core.modules.realsche.dao.TRealscheBoardDao;
import com.dingpeikeji.quickdeer.core.modules.realsche.dao.TRealscheOffDao;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealscheBoard;
import com.dingpeikeji.quickdeer.core.modules.realsche.entity.TRealscheOff;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.realsche.service.FRealscheService;
import com.dingpeikeji.quickdeer.modules.sms.entity.Sms;

/**
 * 用户订单管理Service
 * @author lh
 * @version 2016-06-15
 */
@Service
@Transactional(readOnly = true)
public class TOrdersService extends CrudService<TOrdersDao, TOrders> {
	
	@Autowired
	private TRealscheBoardDao tRealscheBoardDao;
	@Autowired
	private TRealscheOffDao tRealscheOffDao;
	@Autowired
	private FRealscheService fRealscheService;
	@Autowired
	private TOrdersDao tOrdersDao;

	public TOrders get(String id) {
		return super.get(id);
	}
	
	public Sms composeSms(String id){
		Sms sendSms = new Sms();
		
		TOrders orderTmp = this.get(id);	
		if (orderTmp == null)
			return null;
		else
		{
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");
			String dateString = formatter.format(orderTmp.getDepartdate());
			TRealscheBoard board = tRealscheBoardDao.get(orderTmp.getRealboardid());
			TRealscheOff off = tRealscheOffDao.get(orderTmp.getRealoffid());
			sendSms.setMobile(orderTmp.getPayertel());
			if(orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_REALSCHE_SELF_TRAVEL)){
				sendSms.setContent("订单号："+orderTmp.getOrderid()	+"，您已购" + dateString + board.getBoardaddr()+"开往"+off.getOffaddr()+"产品，共" + orderTmp.getPurchasenum()+"份。");
			}
			//AA拼车短信
			if(orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_AIR_SHARE)||orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_TRAVEL_SHARE)){
				sendSms.setContent(
				"【霞客约车】订单号："+orderTmp.getOrderid()+"您已成功购买"+dateString+"  "+
				orderTmp.getOrdername()+"。上车地点 "+orderTmp.getBoardaddr()+",共"+orderTmp.getPaycount()
						+ "份。具体车牌信息请将于拼车成功后发送给您。如有任何疑问，欢迎咨询客服4009287733。"
				);
			}
			if(orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_RENTPRODUCT_AIRPLANE) ||
					orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_RENTPRODUCT_CUSTOM) ||
					orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_RENTPRODUCT_DAY) ||
					orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_RENTPRODUCT_TRAIN) ||
					orderTmp.getOrdertype().equals(TOrders.ORDER_TYPE_RENTPRODUCT_TRAVEL)){
				sendSms.setContent(
						MessageFormat.format("订单号：{0} 您已成功购买{1}，{2}，{3}辆。平台将尽快为您进行派车，提前告知车辆、司机信息，请注意查收。如有任何疑问，欢迎咨询客服4009287733。", 
								orderTmp.getOrderid(),
								orderTmp.getOrdername(),
								orderTmp.getCartype().getTypename(),
								orderTmp.getPurchasenum()
								)
						);
			}
		}
						
		return sendSms;
	}
	public List<TOrders> findList(TOrders tOrders) {
		return super.findList(tOrders);
	}
	
	public Page<TOrders> findPage(Page<TOrders> page, TOrders tOrders) {
		return super.findPage(page, tOrders);
	}
	
	
	public Page<TOrders> findWarnList(Page<TOrders> page, TOrders tOrders) {
		tOrders.setPage(page);
		page.setList(dao.findWarnList(tOrders));
		return page;
	}
	
	
	//客运公司获取得到的订单
	public Page<TOrders> findPageByProvider(Page<TOrders> page, TOrders tOrders) {
		//获取当前用户
		User user = UserUtils.getUser();
		if (!user.isAdmin()&&!user.getName().contains("超级管理员")&&!user.getName().contains("调度")) {
			Office tmp = new Office();
			tmp.setId(user.getOffice().getId());
			tOrders.setOffice(tmp);	
			tOrders.setOrderuserid(user.getId());
		}
		return super.findPage(page, tOrders);
	}
	
	//获取可结算的订单
	public List<TOrders> findSettleList(TOrders tOrders) {
		return dao.findSettleList(tOrders);
	}
	
	//根据结算单号获取订单
	public List<TOrders> findOrderListBySettleId(String settleId) {
		return dao.findOrderListBySettleId(settleId);
	}
	
	@Transactional(readOnly = false)
	public void save(TOrders tOrders) {
		super.save(tOrders);
	}
	
	@Transactional(readOnly = false)
	public void orderback(TOrders tOrders) {
		//车队退单要全额退款
		dao.orderback(tOrders);
	}
	
	@Transactional(readOnly = false)
	public void platback(TOrders tOrders) {
		//平台退单要全额退款
		dao.orderback(tOrders);
	}
	
	@Transactional(readOnly = false)
	public void breakbus(TOrders tOrders) {
		//车队违约全额退款
		dao.updatesettle(tOrders);
	}
	
	@Transactional(readOnly = false)
	public void trouble(TOrders tOrders) {
		dao.trouble(tOrders);
	}
	
	@Transactional(readOnly = false)
	public void confirm(TOrders tOrders) {
		dao.confirm(tOrders);
	}
	
	@Transactional(readOnly = false)
	public void changebus(TOrders tOrders) {
		fRealscheService.proConfirmChgBus(Integer.parseInt(tOrders.getOrderid()), tOrders.getSettlereason());
	}	
		
	@Transactional(readOnly = false)
	public void delete(TOrders tOrders) {
		super.delete(tOrders);
	}

	public List<TOrders> findOfflineOrder(String orderstatus) {
		return tOrdersDao.findOfflineOrder(orderstatus);
	}

	public String findOrderRemarks(String orderid) {
		return tOrdersDao.findOrderRemarks(orderid);
	}

	public Double findDueaMount(String orderid) {
		return tOrdersDao.findDueaMount(orderid);
	}

	public void updateOfferMount(String orderid, String offermount) {
		tOrdersDao.updateOfferMount(orderid,offermount);
	}

	public String findOfferMount(String orderid) {
		return tOrdersDao.findOfferMount(orderid);
	}

	public int delOrder(String orderid) {
		return tOrdersDao.delOrder(orderid);
	}
	public TOrders getOrderByTripId(String tripId){
		return dao.getOrderByTripId(tripId);
	}

	public void delOrderById(String orderid) {
		dao.delOrderById(orderid);
	}

	public void insertSFZ(String sfzCode,String contacts,String payertel,String id, Integer paycount, Integer orderid) {
		dao.insertSFZ(sfzCode,contacts,payertel,id,paycount,orderid);
	}

	public List<TOrders> getOrderByShareId(String id) {
		return dao.getOrderByShareId(id);
	}

	public List<String> getOrderTelByShareId(String id) {
		return dao.getOrderTelByShareId(id);
	}

	public List<String> checkIsCart(String sid, String userid) {
		return dao.checkIsCart(sid,userid);
	}
	
	//客户管理
	public Page<TOrders> findCusmanagePage(Page<TOrders> page, TOrders tOrders) {
		//获取当前用户
		User user = UserUtils.getUser();
		if (!user.isAdmin()) {
			Office tmp = new Office();
			tmp.setId(user.getOffice().getId());
			tOrders.setOffice(tmp);		
		}
		tOrders.setPage(page);
		page.setList(dao.findcus(tOrders));
		return page;
	}
	
	public List<TOrders> findcus(TOrders orders){
		User user = UserUtils.getUser();
		if (!user.isAdmin()) {
			Office tmp = new Office();
			tmp.setId(user.getOffice().getId());
			orders.setOffice(tmp);		
		}
		return dao.findcus(orders);
	}
	
	//财务管理
	public  Page<TFinance> findFinanceList(Page<TFinance> page,TFinance finance){
		User user = UserUtils.getUser();
		if (!user.isAdmin()) {
			Office tmp = new Office();
			tmp.setId(user.getOffice().getId());
			finance.setOffice(tmp);		
		}
		finance.setPage(page);
		page.setList(dao.findFinanceList(finance));
		return page;
	}
	public TFinance getFinance(TFinance finance){
		return dao.getFinance(finance);
	}
	public void saveFinance(TFinance finance){
		dao.saveFinance(finance);
	}

	public void editOrder(TOrders orders) {
		tOrdersDao.editOrder(orders);
	}

}
